// Set config defaults when creating the instance
import axios from "axios";

const isDev = process.env.NODE_ENV === 'development';


const myAxios = axios.create({
    baseURL: isDev ? 'http://localhost:3001/api' : 'https://partner.coder-zyb.top/api',
    withCredentials: true,
});

// Add a request interceptor
myAxios.interceptors.request.use(function (config) {
    console.log("发送请求了",config)
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
myAxios.interceptors.response.use(function (response) {
    console.log("接收到响应了",response)
    // 如果是未登录，则需要跳转登录页
    if(response?.data?.code===40100){
        // 记录是存哪个界面跳转的登录页，需要进行重定向
        const redirectUrl = window.location.href
        console.log("redirectUrl",redirectUrl)
        window.location.href=`/user/login?redirect=${redirectUrl}`
    }
    // Do something with response data
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default myAxios;
